package com.lancoo.answer.view.adapter.wordSpell;

import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.DynamicDrawableSpan;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lancoo.answer.R;
import com.lancoo.answer.model.entity.wordSpell.WordListenWriteChild;
import com.lancoo.answer.model.eventBean.KlgEvenBean;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by pengmi on 2022/1/20 15:14
 * Description:词汇拼写查看结果adapter
 */
public class WordSpellResultAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<WordListenWriteChild> list;

    public WordSpellResultAdapter(List<WordListenWriteChild> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.ev_item_word_spell_result, parent, false);
        return new VH(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        VH vh= (VH) holder;
        vh.initData(list.get(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    private static class VH extends RecyclerView.ViewHolder implements View.OnClickListener{

        private WordListenWriteChild writeChild;

        public VH(@NonNull View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
        }

        private void initData(WordListenWriteChild writeChild){
            this.writeChild=writeChild;
            TextView tv_word = itemView.findViewById(R.id.tv_word);
            TextView tv_answer = itemView.findViewById(R.id.tv_answer);
            String wordStr = writeChild.getWordStr();
            String wordCode = writeChild.getWordCode();
            String answerStr = writeChild.getAnswerStr();
            boolean pass = writeChild.isPass();
            SpannableString spannableString=new SpannableString("  "+wordStr.replace("-","- "));
            if(!TextUtils.isEmpty(wordCode)){
                ImageSpan imageSpan=new ImageSpan(itemView.getContext(),
                        R.mipmap.ev_ic_word_listen_write_look_up, DynamicDrawableSpan.ALIGN_BASELINE);
                spannableString.setSpan(imageSpan, 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
            }
            tv_word.setText(spannableString);
            if(TextUtils.isEmpty(answerStr)||TextUtils.isEmpty(answerStr.trim())){
                tv_answer.setText("未作答");
//                tv_word.setTextColor(0xff989898);
                tv_word.setTextColor(0xff0099ff);
                tv_answer.setTextColor(0xff989898);
            }else{
                tv_answer.setText(answerStr);
                tv_word.setTextColor(0xff0099ff);
                tv_answer.setTextColor(pass?0xff009900:0xffff0000);
            }

        }

        @Override
        public void onClick(View v) {
            int position=getAdapterPosition();
            if(writeChild!=null&&position>=0){
                KlgEvenBean bean=new KlgEvenBean();
                bean.setKlgCode(writeChild.getWordCode());
                bean.setKlgName(writeChild.getWordStr());
                bean.setIndex(position);
                EventBus.getDefault().post(bean);
            }

        }
    }
}
